//
//  TLLoginViewController.m
//  FunnyVideo
//
//  Created by 李鑫 on 15/10/19.
//  Copyright © 2015年 NewEggTech. All rights reserved.
//

#import "TLLoginViewController.h"
#import "AVOSUserManger.h"
#import "TLUserManger.h"
#import "TLShareCenter.h"
#import "AVOSCloudSNS.h"
@implementation TLLoginViewController

+ (TLLoginViewController *)sharedLoginViewController{
    static dispatch_once_t onceToken;
    static TLLoginViewController *loginVC;
    dispatch_once(&onceToken, ^{
        loginVC = [TLLoginViewController sharedCtrlWithIdentifier:@"LoginVC"];
    });
    
    return loginVC;
}

+ (void)showLoginView:(UINavigationController *)navCtrl completion:(void(^)(BOOL isLogin))completion{
    TLLoginViewController *loginVC = [TLLoginViewController sharedLoginViewController];
    [navCtrl presentViewController:loginVC animated:YES completion:^{
        completion(YES);
    }];
}

- (IBAction)showFGPswVC:(id)sender {
    
}

- (IBAction)login:(id)sender {
    [self showLoading];
    BOOL userNameInput = [self checkUserTF];
    if(!userNameInput)
    {
        [TLAlertHelper showErrorHUDWithText:@"请输入用户名"];
        return;
    }
    
    BOOL userPswInput = [self checkUserPSW];
    if(!userPswInput)
    {
        [TLAlertHelper showErrorHUDWithText:@"请输入密码"];
        return;
    }
    
    [AVOSUserManger loginWithMobile:self.userNameTF.text Psw:self.pswTF.text completion:^(AVUser *user, BOOL succ,NSError *error) {
        if(user)
        {
            dispatch_async(dispatch_get_main_queue(), ^{
            [[TLUserManger sharedManger] setAVUser:user];
            [self dismissViewControllerAnimated:YES completion:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                [TLAlertHelper showSuccessHUDWithText:@"登陆成功"];
                });
            }];
            });
        }
        else
        {
            [TLAlertHelper showErrorHUDWithText:[error userInfo][@"NSLocalizedDescription"]];
        }
        
        [self dismissLoading];
    }];
}

- (IBAction)wxLogin:(id)sender {
    
    
    //微信appID             //secret key
    //李鑫的 wx2cd66644e3c30d9f    //d4624c36b6795d1d99dcf0547af5443d
    //森哥的  wx78c41f313e22f305    //54f583ff8941055f4514ac9bbb4439e5
   [AVOSCloudSNS setupPlatform:AVOSCloudSNSWeiXin withAppKey:@"wx78c41f313e22f305" andAppSecret:@"54f583ff8941055f4514ac9bbb4439e5" andRedirectURI:@""];
    
   [AVOSCloudSNS loginWithCallback:^(id object, NSError *error) {
            
            [AVUser loginWithAuthData:object platform:AVOSCloudSNSPlatformWeiXin block:^(AVUser *user, NSError *error) {
                if (error) {
                    // 登录失败，可能为网络问题或 authData 无效
                    [TLAlertHelper showErrorHUDWithText:@"登录失败"];
                } else {
                    // 登录成功
                    [TLAlertHelper showSuccessHUDWithText:@"登陆成功"];
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self dismissViewControllerAnimated:YES completion:^{
                            
                        }];
                    });
                    
                }
            }];
        } toPlatform:AVOSCloudSNSWeiXin];
}

- (IBAction)usernameEditingEnd:(id)sender {
    [self.pswTF becomeFirstResponder];
}

- (IBAction)pswTFEditingEnd:(id)sender {
    [self.pswTF resignFirstResponder];
    
    [self login:sender];
}

#pragma mark - User Input Check
- (BOOL)checkUserPSW{
    return (self.pswTF.text.length > 0);
}

- (BOOL)checkUserTF{
    return (self.userNameTF.text.length > 0);
}


#pragma mark -- View Changed
- (void)showLoading{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.loginIndicator setHidden:NO];
        [self.loginIndicator startAnimating];
    });
}

- (void)dismissLoading{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.loginIndicator setHidden:YES];
        [self.loginIndicator stopAnimating];
    });
}

@end
